package cn.iocoder.yudao.module.security.service.asset;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.net.Ipv4Util;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.security.util.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.security.controller.admin.asset.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.asset.SecurityAssetDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.security.dal.mysql.asset.SecurityAssetMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.security.enums.ErrorCodeConstants.*;

/**
 * 公司资产管理 Service 实现类
 *
 * @author cai
 */
@Service
@Validated
public class SecurityAssetServiceImpl implements SecurityAssetService {

    private static final Logger log = LoggerFactory.getLogger(SecurityAssetServiceImpl.class);
    @Resource
    private SecurityAssetMapper assetMapper;

    @Override
    public Long createAsset(SecurityAssetSaveReqVO createReqVO) {
        List<SecurityAssetDO> list = assetMapper.selectList(
                new LambdaQueryWrapperX<SecurityAssetDO>()
                        .eq(SecurityAssetDO::getIpAddress, createReqVO.getIpAddress())
                        .eq(SecurityAssetDO::getDeleted, false)
        );
        if (CollectionUtil.isNotEmpty(list)) {
            throw exception(ASSET_EXISTS);
        }
        // 插入
        SecurityAssetDO asset = BeanUtils.toBean(createReqVO, SecurityAssetDO.class);
        asset.setSourceType("2");
        asset.setIpLong(IpUtils.ipv4ToLong(asset.getIpAddress()));
        asset.setStatus(CommonStatusEnum.ENABLE.getStatus());
        assetMapper.insert(asset);
        // 返回
        return asset.getId();
    }

    @Override
    public void updateAsset(SecurityAssetSaveReqVO updateReqVO) {
        // 校验存在
        validateAssetExists(updateReqVO.getId());
        // 更新
        if (Objects.isNull(updateReqVO.getIpLong())) {
            updateReqVO.setIpLong(IpUtils.ipv4ToLong(updateReqVO.getIpAddress()));
        }
        SecurityAssetDO updateObj = BeanUtils.toBean(updateReqVO, SecurityAssetDO.class);
        assetMapper.updateById(updateObj);
    }

    @Override
    public void deleteAsset(Long id) {
        // 校验存在
        validateAssetExists(id);
        // 删除
        assetMapper.deleteById(id);
    }

    private void validateAssetExists(Long id) {
        if (assetMapper.selectById(id) == null) {
            throw exception(ASSET_NOT_EXISTS);
        }
    }

    @Override
    public SecurityAssetDO getAsset(Long id) {
        return assetMapper.selectById(id);
    }

    @Override
    public PageResult<SecurityAssetDO> getAssetPage(SecurityAssetPageReqVO pageReqVO) {
        if (StringUtils.isNotBlank(pageReqVO.getStartIp()) && StringUtils.isNotBlank(pageReqVO.getEndIp())) {
            if (IpUtils.isIpv4Address(pageReqVO.getStartIp()) && IpUtils.isIpv4Address(pageReqVO.getEndIp())) {
                long startIp = IpUtils.ipv4ToLong(pageReqVO.getStartIp());
                long endIp = IpUtils.ipv4ToLong(pageReqVO.getEndIp());
                Long[] ipArr = {startIp, endIp};
                pageReqVO.setIpLongArr(ipArr);
            } else {
                log.error("ip地址格式不正确");
            }
        }
        return assetMapper.selectPage(pageReqVO);
    }

    @Override
    public void confirmAssetBatch(List<Long> reqVO) {
        List<Long> ids = reqVO;
        if (CollectionUtil.isEmpty(ids)) {
            throw exception(ASSET_NOT_EXISTS);
        }
        List<SecurityAssetDO> list = new ArrayList<>();
        for (Long id : ids) {
            SecurityAssetDO asset = assetMapper.selectById(id);
            if (Objects.isNull(asset)) {
                throw exception(ASSET_NOT_EXISTS);
            }
            asset.setStatus(CommonStatusEnum.ENABLE.getStatus());
            list.add(asset);
        }
        assetMapper.updateBatch(list);
    }

}